package com.xnf.ssmweb.controller;

import com.xnf.ssmweb.domain.Product;
import com.xnf.ssmweb.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import java.lang.reflect.Method;
import java.util.List;

/**
 * Created by chendx on 2021/3/26.
 */
@Controller
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @RequestMapping("/toMain")
    public ModelAndView toMain(){
        ModelAndView mav = new ModelAndView();
        mav.setViewName("/main");
        return mav;
    }


    @RequestMapping("/toProduct")
    public ModelAndView toProduct(){
        List<Product> products = productService.queryProductList();
        System.out.println("toProduct.......");
        ModelAndView mav = new ModelAndView();
        mav.addObject("products",products);
        mav.setViewName("/product-list");
        return mav;
    }

    @RequestMapping(value = "/saveProduct")
    public ModelAndView saveProduct(Product product){

        System.out.println(product.toString());
        boolean flag = productService.saveProduct(product);
        ModelAndView mav = new ModelAndView();
        if (flag){
            mav.setViewName("redirect:/product/toProduct");
        }else {
            mav.setViewName("/product-add");
        }
        return mav;
    }

}
